using System.Collections;
using System.Collections.Generic;
using QFramework.Example;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

namespace QFramework
{
    public class UISlot : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler, IPointerEnterHandler, IPointerExitHandler
    {
        public Image Icon;
        public Text Count;

        public Slot Data { get; private set; }

        private bool mDragging = false;

        public UISlot InitWithData(Slot slot)
        {
            Data = slot;

            void UpdateView()
            {
                if (slot.Count <= 0)
                {
                    Icon.Hide();
                    Count.text = "";
                }
                else
                {
                    if (Data.Item.IsStackable) // 可堆叠
                    {
                        Count.text = Data.Count.ToString();
                        Count.Show();
                    }
                    else // 不可堆叠
                    {
                        Count.Hide();
                    }
                    Icon.Show();
                    if (Data.Item.GetIcon)
                    {
                        Icon.sprite = Data.Item.GetIcon;
                    }
                    Count.text = slot.Count.ToString();
                }
            }
            
            Data.Changed.Register(() =>
            {
                UpdateView();
            }).UnRegisterWhenGameObjectDestroyed(gameObject);
            
            UpdateView();
            return this;
        }

        public void OnBeginDrag(PointerEventData eventData)
        {
            if (mDragging || Data.Count <= 0) return;
            mDragging = true;

            var canvas = Icon.GetOrAddComponent<Canvas>();
            canvas.overrideSorting = true;
            canvas.sortingOrder = 1000;
            
            SyncItemToMousePos();
        }

        public void OnDrag(PointerEventData eventData)
        {
            if (mDragging)
            {
                SyncItemToMousePos();
            }
        }

        public void OnEndDrag(PointerEventData eventData)
        {
            if (mDragging)
            {
                mDragging = false;
                var canvas = Icon.GetComponent<Canvas>();
                canvas.DestroySelf();
                
                Icon.LocalPositionIdentity(); // 重置位置
                
                if (ItemKit.CurrentSlotPointerOn)
                {
                    var uiSlot = ItemKit.CurrentSlotPointerOn;
                    RectTransform rectTransform = uiSlot.transform as RectTransform;

                    if (RectTransformUtility.RectangleContainsScreenPoint(rectTransform, Input.mousePosition))
                    {
                        // 有物品
                        if (Data.Count > 0)
                        {
                            // 能放到目标位置
                            if (uiSlot.Data.Group.CheckCondition(Data.Item))
                            {
                                // 交换物品
                                IItem cacheItem = uiSlot.Data.Item;
                                int cacheCount = uiSlot.Data.Count;
                                uiSlot.Data.Item = Data.Item;
                                uiSlot.Data.Count = Data.Count;
                                Data.Item = cacheItem;
                                Data.Count = cacheCount;

                                Data.Changed.Trigger();
                                uiSlot.Data.Changed.Trigger();
                            }
                        }
                    }
                }
                else
                {
                    Data.Item = null;
                    Data.Count = 0;
                    
                    Data.Changed.Trigger();
                }
            }
        }

        /// <summary>
        /// 文本可以受到拖拽
        /// </summary>
        void SyncItemToMousePos()
        {
            var mousePos = Input.mousePosition;
            
            if (RectTransformUtility.ScreenPointToLocalPointInRectangle(transform as RectTransform,
                    mousePos,
                    null,
                    out var localPos))
            {
                Icon.LocalPosition2D(localPos);
            }
        }

        public void OnPointerEnter(PointerEventData eventData)
        {
            ItemKit.CurrentSlotPointerOn = this;
        }

        public void OnPointerExit(PointerEventData eventData)
        {
            if (ItemKit.CurrentSlotPointerOn == this)
            {
                ItemKit.CurrentSlotPointerOn = null;
            }
        }
    }
}
